﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dataglot;
using NUnit.Framework;

namespace IrishSettings.IntegrationTests.SchemaManagerTests
{
    [TestFixture]
    public class SchemaUpgradeTests
    {
        [SetUp]
        public void Setup()
        {
            TestSettings app = new TestSettings();
            DbApplicationManager appManager = new DbApplicationManager(ConfigUtils.DefaultDataFactory());

            appManager.DeleteApplication(app.Application, app.VersionObject);
        }


        [Test]
        public void ImmediatelyAfterUpgradeNothingChanges()
        {
            //Defaults
            TestSettings app = SchemaUtils.CreateSampleSchema();
            IrishSettingsManager hsm = new IrishSettingsManager(app);
            hsm.Save();

            DataFactory factory = ConfigUtils.DefaultDataFactory();
            DbApplicationManager appManager = new DbApplicationManager(factory);
            Version newVersion = new Version(2, 1);
            appManager.Upgrade(app.Application,newVersion);
            DbSchemaManager schemaManager = new DbSchemaManager(app, factory);
            SettingsSchema newSchema= schemaManager.LoadSchemaForCurrentEnvironment(newVersion);
            hsm = new IrishSettingsManager(app);

            Assert.AreEqual("en-US", hsm["culture"].Value.ToString());

            Assert.IsNotNull(hsm["culture"]);
            Assert.AreEqual("en-US", hsm["culture"].Value.ToString());
            hsm.Save();
        }


        //[Test]
        //public void BasicUpgrade()
        //{
        //    //Defaults
        //    IrishSettingsManager hsm = new IrishSettingsManager(SchemaUtils.CreateSampleSchema());
        //    hsm.Save();
        //    hsm.Upgrade(new Version(2, 1));
        //    Assert.AreEqual("en-US", hsm["culture"].Value.ToString());
        //    Assert.IsNotNull(hsm["culture"]);
        //    Assert.AreEqual("en-US", hsm["culture"].Value.ToString());
        //    hsm.Save();
        //    hsm["culture"].Value = "is-is";
        //    hsm.Save();
        //    Assert.AreEqual("is-is", hsm["culture"].Value.ToString());
        //}

        //[Test]
        //public void BasicUpgradeAndChange()
        //{
        //    //Defaults
        //    IrishSettingsManager hsm = new IrishSettingsManager(SchemaUtils.CreateSampleSchema());
        //    hsm.Save();
        //    hsm.Upgrade(new Version(2, 1));
        //    Assert.AreEqual("en-US", hsm["culture"].Value.ToString());
        //    Assert.IsNotNull(hsm["culture"]);
        //    Assert.AreEqual("en-US", hsm["culture"].Value.ToString());
        //    hsm.Save();
        //    hsm["culture"].Value = "is-is";
        //    hsm.Save();
        //    IrishSettingsManager hsm2 = new IrishSettingsManager(SchemaUtils.CreateSampleSchema(new Version(2, 1)));
        //    Assert.AreEqual("is-is", hsm2["culture"].Value.ToString());

        //    IrishSettingsManager hsm3 = new IrishSettingsManager(SchemaUtils.CreateSampleSchema(new Version(1, 0)));
        //    Assert.AreEqual("en-US", hsm3["culture"].Value.ToString());
        //}


        //[Test]
        //public void GetCurrentAndLastAfterUpdgrade()
        //{
        //    //Defaults
        //    IrishSettingsManager hsm = new IrishSettingsManager(SchemaUtils.CreateSampleSchema());
        //    //Make sure nothing else is there.
        //    hsm.Reset();
        //    hsm.Reset(new Version(1, 0));
        //    hsm.Reset(new Version(2, 1));

        //    //Save forces, v1 to save
        //    hsm.Save();
        //    hsm.Upgrade(new Version(2, 1));
        //    //Save, now v2 has saved.
        //    hsm.Save();
        //    Assert.AreEqual("en-US", hsm["culture"].Value.ToString());
        //    Assert.AreEqual(new Version(2, 1).ToString(), hsm.VersionCurrent().ToString());
        //    Assert.AreEqual(new Version(1, 0).ToString(), hsm.VersionPrevious().ToString());
        //}

    }
}
